package SingList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2024-01-30
 * Time: 18:27
 */
public class MyList implements IList {

    static class ListNode {
        private int value;
        private ListNode next;

        public ListNode(int value) {
            this.value = value;
        }
    }

    private ListNode head;// 头节点

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (this.head == null) {
            this.head = node;
        } else {
            node.next = this.head;
            this.head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (this.head == null) {
            this.head = node;
        } else {
            ListNode cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    // 寻找前一个节点
    private ListNode searchPrev(int index) {
        ListNode cur = this.head;
        int count = 0;
        while (count != index - 1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    @Override
    public void addIndex(int index, int data) {
        if (index < 0 || index > size()) {
            System.out.println("下标不合法!");
            return;
        }
        if (index == 0) {
            // 相当于头插法
            addFirst(data);
            return;
        }
        if (index == size()) {
            // 相当于尾插法
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = searchPrev(index);
        node.next = cur.next;
        cur.next = node;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.value == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        // 如果没有节点
        if (this.head == null) {
            return;
        }

        // 如果头节点的值为key
        if (this.head.value == key) {
            this.head = this.head.next;
            return;
        }

        // 找到前一个节点
        ListNode pre = FindPrev(key);
        if (pre == null) {
            System.out.println("没有找到要删除的节点!");
            return;
        }
        ListNode del = pre.next;
        pre.next = del.next;

    }

    // 根据 key 找前一个节点
    public ListNode FindPrev(int key) {
        ListNode cur = this.head;
        while (cur.next != null) {
            if (cur.next.value == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {
        // 判断头节点是否为空
        if (this.head == null) {
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;

        while (cur != null) {
            if (cur.value == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        // 判断头节点是否为 key
        if (this.head.value == key) {
            this.head = this.head.next;
            return;
        }
    }

    @Override
    public int size() {
        int number = 0;
        ListNode cur = this.head;
        while (cur.next != null) {
            number++;
            cur = cur.next;
        }
        return number;
    }

    @Override
    public void clear() {
        if (this.head == null) {
            return;
        }
        ListNode cur = this.head;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }
        head = null;
    }

    @Override
    public void display() {
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
